package com.cy.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created with IDEA2020.3.3 JDK1.8 Mysql 5.7 maven3.6.3
 *
 * @Auther: 张鹏宇
 * @Date: 2021/04/09/15:15
 * @Description:
 */
public class MybatisUtils {
    private static SqlSessionFactory factory = null;

    static {
        //名字需要一致
        String config = "mybatis.xml";
        try {
            InputStream in = Resources.getResourceAsStream (config);

            //创建 对象
            factory = new SqlSessionFactoryBuilder ().build (in);

        } catch (IOException e) {
            e.printStackTrace ();
        }
    }


    public static SqlSession getSqlSession () {
        SqlSession sqlSession = null;

        if (factory != null) {
            //非自动提交事物
            sqlSession = factory.openSession ();
        }
        return sqlSession;
    }
}
